import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import {
  getUBSPosition,
  getStockRunPoint,
  getStockList,
  addStock,
  deleteStock,
  getFund2PlanStockDateList,
} from '../../../../services/allposition_service';
import { searchCode } from '../../../../services/line_service';

export default {
  namespace: 'ed',
  state: {
    codeList: [],
    symbolList: [],
    selectDate: [],
  },

  effects: {
    *SearchCode({ payload, callback }, { call, put }) {
      const response = yield call(searchCode, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined') {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        yield put({
          type: 'saveCodeList',
          payload: response.data.splice(0, 6),
        });
        if (callback) callback(response);
      }
    },
    *GetStockList({ payload, callback }, { call, put }) {
      const response = yield call(getStockList, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined') {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        yield put({
          type: 'saveSymbolList',
          payload: response.data,
        });
      }
    },
    *GetFund2PlanStockDateList({ payload, callback }, { call, put }) {
      const response = yield call(getFund2PlanStockDateList, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined') {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        yield put({
          type: 'saveSelectDate',
          payload: response.data,
        });
      }
    },
    *AddStock({ payload, callback }, { call, put }) {
      const response = yield call(addStock, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined') {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
      }
    },
    *DeleteStock({ payload, callback }, { call, put }) {
      const response = yield call(deleteStock, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined') {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
      }
    },

    // stock
    *GetUBSPosition({ payload, callback }, { call, put }) {
      const response = yield call(getUBSPosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // 添加敞口
        let allstockopen = 0;
        for (let i = 0; i < response.list.positionList.length; i++) {
          allstockopen += response.list.positionList[i].costmoney || 0;
        }
        for (let i = 0; i < response.list.positionList.length; i++) {
          response.list.positionList[i].stockopen =
            (response.list.positionList[i].costmoney || 0) / allstockopen;
        }
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // CHARTS
    *GetStockRunPoint({ payload, callback }, { call, put }) {
      const response = yield call(getStockRunPoint, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        // debugger
        const res = { codes: response.data.codes, list: [] };
        const { list } = response.data;
        for (let j = 0; j < list.length; j++) {
          const dateJ = (list[j].date || '').split(' ')[0];
          res.list.push({
            type: '股票净值',
            x: dateJ,
            value: list[j].percent,
          });
          res.list.push({
            type: 'SPY净值',
            x: dateJ,
            value: list[j].spyPercent,
          });
          // res.list.push({
          //   type:'相对强度2',
          //   x:dateJ,
          //   value:list[j].percent/list[j].spyPercent +1,
          // });
          // res.list.push({
          //   type: '相对强度',
          //   x: dateJ,
          //   value: list[j].relativestrengthline,
          // });
          // res.list.push({
          //   type: '20日相对强度',
          //   x: dateJ,
          //   value: list[j].relativestrengthline20,
          // });
          res.list.push({
            type: '跑赢SPY比率(1周）',
            x: dateJ,
            value: list[j].oneweek,
          });
          res.list.push({
            type: '跑赢SPY比率(2周）',
            x: dateJ,
            value: list[j].twoweek,
          });
          res.list.push({
            type: '跑赢SPY比率(1月）',
            x: dateJ,
            value: list[j].onemonth,
          });
          res.list.push({
            type: '跑赢SPY比率(3月）',
            x: dateJ,
            value: list[j].threemonth,
          });
          res.list.push({
            type: '跑赢SPY比率(6月）',
            x: dateJ,
            value: list[j].sixmonth,
          });
          res.list.push({
            type: '跑赢SPY比率(1年）',
            x: dateJ,
            value: list[j].oneyear,
          });
        }
        if (callback) callback(res);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveCodeList(state, action) {
      return {
        ...state,
        codeList: action.payload,
      };
    },
    saveSymbolList(state, action) {
      return {
        ...state,
        symbolList: action.payload,
      };
    },
    saveSelectDate(state, action) {
      return {
        ...state,
        selectDate: action.payload,
      };
    },

    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },
  },
};
